public class test2 {
    public static int maxSubArray(int[] nums) {
        int max = nums[0];
        int current = nums[0];

        for (int i = 1; i < nums.length; i++) {
            current = Math.max(nums[i], current + nums[i]);
            max = Math.max(max, current);
        }

        return max;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, -2, 3, 5, -1};        // 返回8
        int[] arr2 = {1, -2, 3, -8, 5, 1};     // 返回6
        int[] arr3 = {1, -2, 3, -2, 5, 1};     // 返回7

        System.out.println(maxSubArray(arr1)); // 8
        System.out.println(maxSubArray(arr2)); // 6
        System.out.println(maxSubArray(arr3)); // 7
    }
}
